package com.dom.domweb.dao.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dom.common.constants.ConstantError;
import com.dom.common.enums.PubStatEnum;
import com.dom.common.model.vo.basic.*;
import com.dom.domweb.common.RspModel;
import com.dom.domweb.dao.entity.TGoodStandardTbl;
import com.dom.domweb.dao.entity.TGoodVarietyTbl;
import com.dom.domweb.dao.mapper.TGoodStandardTblMapper;
import com.dom.domweb.dao.service.ITGoodStandardTblService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * B_商品规格代码表 服务实现类
 * </p>
 *
 * @author xllei
 * @since 2018-11-17
 */
@Service
public class TGoodStandardTblServiceImpl extends ServiceImpl<TGoodStandardTblMapper, TGoodStandardTbl> implements ITGoodStandardTblService {

    @Override
    public B129A getStandardInfoList(B129R request) {
        Wrapper<TGoodStandardTbl> wrapper = new EntityWrapper();
        Page<TGoodStandardTbl> page = new Page<>();
        page.setCurrent(request.getPn());
        page.setSize(request.getPs());
        page = this.selectPage(page,wrapper);
        B129A result = new B129A();
        result.setTotalNum((int)page.getTotal());
        List<StandardInfo> list = infoToTbl(page.getRecords());
        result.setStandardData(list);
        return result;
    }

    private List<StandardInfo> infoToTbl(List<TGoodStandardTbl> list) {
        List<StandardInfo> result = new ArrayList<>();
        for (TGoodStandardTbl tblInfo:  list) {
            StandardInfo info = new StandardInfo();
            info.setId(tblInfo.getId().toString());
            info.setMemCode(tblInfo.getMemCode());
            info.setStandardCode(tblInfo.getStandardCode());
            info.setStandardName(tblInfo.getStandardName());
            info.setStat(tblInfo.getStat().toString());
            info.setStatName(PubStatEnum.getName(tblInfo.getStat()));
            result.add(info);
        }
        return result;
    }

    @Override
    public Map<String,Object> getAllStandardInMap(){
        Wrapper<TGoodStandardTbl> wrapper = new EntityWrapper();
        List<TGoodStandardTbl> list = this.baseMapper.selectList(wrapper);
        Map<String,Object> map = new HashMap<String,Object>();
        for (TGoodStandardTbl tbl:list ) {
            map.put(tbl.getStandardCode().toString(),tbl.getStandardName());
        }
        return map;
    }

    @Override
    public B129A getAllStandardInfo() {
        B129A b = new B129A();
        Wrapper<TGoodStandardTbl> wrapper = new EntityWrapper();
        List<TGoodStandardTbl> list = this.baseMapper.selectList(wrapper);
        List<StandardInfo> result = infoToTbl(list);
        b.setStandardData(result);
        return b;
    }

    @Override
    public B123A getStandardInfoByCode(String id) {
        TGoodStandardTbl tbl =  this.baseMapper.selectById(id);
        B123A result = new B123A();
        result.setStandardCode(tbl.getStandardCode().toString());
        result.setId(tbl.getId().toString());
        result.setStandardName(tbl.getStandardName());
        result.setStat(tbl.getStat().toString());
        return result;
    }

    @Override
    public RspModel insertStandardInfo(B120R request) {
        Wrapper<TGoodStandardTbl> wrapper = new EntityWrapper<TGoodStandardTbl>();
        wrapper.eq("standard_code",request.getStandardCode());
        List<TGoodStandardTbl> list = this.baseMapper.selectList(wrapper);
        if (list != null && list.size() != 0){
            return new RspModel("RSP000030","商品规格已存在！");
        }
        TGoodStandardTbl tblInfo = new TGoodStandardTbl();
        tblInfo.setStat(PubStatEnum.STAT_UNACTIVATED.getCode());
        tblInfo.setMemCode(0);
        tblInfo.setStandardCode(StringUtils.isEmpty(request.getStandardCode()) ? null : Integer.valueOf(request.getStandardCode()));
        tblInfo.setStandardName(request.getStandardName());
        tblInfo.setVersion(0);
        int count = this.baseMapper.insert(tblInfo);
        RspModel model = new RspModel("RSP000000","商品规格信息新增成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }

    @Override
    public RspModel editStandardInfo(B120R request) {
        Wrapper<TGoodStandardTbl> wrapper = new EntityWrapper<TGoodStandardTbl>();
        wrapper.eq("id",request.getId());
        TGoodStandardTbl tbl = this.baseMapper.selectById(request.getId());
        if (tbl.getStat() != PubStatEnum.STAT_UNACTIVATED.getCode()
                &&  !request.getStandardCode().equals(Integer.toString(tbl.getStandardCode())) ){
            return new RspModel(ConstantError.NOT_CODE_ERROR);
        }
        TGoodStandardTbl tblInfo = new TGoodStandardTbl();
        tblInfo.setStandardName(request.getStandardName());
        tblInfo.setStandardCode(StringUtils.isEmpty(request.getStandardCode()) ? null : Integer.valueOf(request.getStandardCode()));
        tblInfo.setStat(StringUtils.isEmpty(request.getStat()) ? PubStatEnum.STAT_UNACTIVATED.getCode() : Integer.valueOf(request.getStat()));
        int count = this.baseMapper.update(tblInfo,wrapper);
        RspModel model = new RspModel("RSP000000","商品规格信息修改成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }

    @Override
    public RspModel standardInfoDel(String id) {
        Wrapper<TGoodStandardTbl> wrapper = new EntityWrapper<TGoodStandardTbl>();
        wrapper.eq("id",id);
        TGoodStandardTbl tblInfo = new TGoodStandardTbl();
        tblInfo.setStat(PubStatEnum.STAT_STOP.getCode());
        int count = this.baseMapper.update(tblInfo,wrapper);
        RspModel model = new RspModel("RSP000000","商品规格删除成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }
}
